home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #5 & #6 / Amiga Plus CD - 1995 - No. 5 and 6.iso / pd / netz / term / extras / source / term-source.lha / termResponse.c < prev    next >
C/C++ Source or Header  |  1995-04-14  |  3KB  |  191 lines

  1. /*
  2. **    termResponse.c
  3. **
  4. **    Signal event handling routines
  5. **
  6. **    Copyright © 1990-1995 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. */
  9.  
  10. #include "termGlobal.h"
  11.  
  12.     /* Yet another function pointer. */
  13.  
  14. typedef BYTE (* RESPONSE)(VOID);
  15.  
  16.     /* Signal response table entry. */
  17.  
  18. struct SignalResponseInfo
  19. {
  20.     RESPONSE    Routine;
  21.     ULONG        Mask;
  22. };
  23.  
  24.     /* Signal response table. */
  25.  
  26. STATIC struct SignalResponseInfo    ResponseTable[7];
  27. STATIC WORD                ResponseCount;
  28. STATIC ULONG                ResponseMask;
  29.  
  30. #ifdef DATAFEED
  31. BPTR DataFeed;
  32. #define CHUNK_LENGTH    20
  33. #define CHAR_DELAY    (1000000/100000)
  34. //#define CHAR_DELAY    0
  35. #endif    /* DATAFEED */
  36.  
  37.     /* AddResponse(RESPONSE Routine,ULONG Mask):
  38.      *
  39.      *    Register a signal response routine.
  40.      */
  41.  
  42. STATIC VOID __regargs
  43. AddResponse(RESPONSE Routine,ULONG Mask)
  44. {
  45.     ResponseTable[ResponseCount] . Routine    = Routine;
  46.     ResponseTable[ResponseCount] . Mask    = Mask;
  47.  
  48.     ResponseMask |= Mask;
  49.  
  50.     ResponseCount++;
  51. }
  52.  
  53.     /* HandleResponse():
  54.      *
  55.      *    Register routines and corresponding signals,
  56.      *    wait for events and process them.
  57.      */
  58.  
  59. VOID
  60. HandleResponse()
  61. {
  62.     register ULONG    SignalSet;
  63.     register BYTE    Running,
  64.             SerialRunning;
  65.     register WORD    i;
  66.  
  67.     ResponseMask    = XEM_Signal;
  68.     ResponseCount    = 0;
  69.  
  70.     AddResponse((RESPONSE)HandleSerialCheck,SIG_CHECK);
  71.  
  72.     if(WorkbenchPort)
  73.         AddResponse((RESPONSE)HandleWorkbenchWindow,SIG_WORKBENCH);
  74.  
  75.     if(ReadPort && Status != STATUS_HOLDING && ProcessIO)
  76.     {
  77.         AddResponse(HandleSerial,SIG_SERIAL);
  78.  
  79.         SerialRunning = TRUE;
  80.     }
  81.     else
  82.         SerialRunning = FALSE;
  83.  
  84.     if(OwnDevUnitBase && !Config -> SerialConfig -> Shared && OwnDevBit != -1)
  85.         AddResponse(HandleOwnDevUnit,1L << OwnDevBit);
  86.  
  87.     AddResponse(HandleWindow,SIG_WINDOW);
  88.  
  89.     if(TermRexxPort)
  90.         AddResponse(HandleRexx,SIG_REXX);
  91.  
  92.     AddResponse(HandleQueueMsg,SIG_QUEUE);
  93.  
  94. #ifdef DATAFEED
  95.         /* Wait for events. */
  96.  
  97.     if(HostReadBuffer || DataHold)
  98.     {
  99.         if(ReadPort && Status != STATUS_HOLDING)
  100.             SignalSet = CheckSignal(ResponseMask) | SIG_SERIAL;
  101.         else
  102.             SignalSet = CheckSignal(ResponseMask);
  103.     }
  104.     else
  105.     {
  106.         if(DataFeed)
  107.             SignalSet = CheckSignal(ResponseMask);
  108.         else
  109.             SignalSet = Wait(ResponseMask);
  110.     }
  111. #else
  112.         /* Wait for events. */
  113.  
  114.     if(HostReadBuffer || DataHold)
  115.     {
  116.         if(ReadPort && Status != STATUS_HOLDING)
  117.             SignalSet = CheckSignal(ResponseMask) | SIG_SERIAL;
  118.         else
  119.             SignalSet = CheckSignal(ResponseMask);
  120.     }
  121.     else
  122.         SignalSet = Wait(ResponseMask);
  123. #endif    /* DATAFEED */
  124.  
  125.     if(SignalSet & XEM_Signal)
  126.         HandleExternalEmulation();
  127.  
  128.     FOREVER
  129.     {
  130. #ifdef DATAFEED
  131.         if(DataFeed)
  132.         {
  133.             UBYTE    LocalBuffer[CHUNK_LENGTH];
  134.             LONG    Len;
  135.  
  136.             if((Len = Read(DataFeed,LocalBuffer,CHUNK_LENGTH)) > 0)
  137.             {
  138.                 STRPTR Index = LocalBuffer;
  139.  
  140.                 if(Marking)
  141.                     DropMarker();
  142.  
  143.                 if(CHAR_DELAY)
  144.                 {
  145.                     do
  146.                     {
  147.                         ConProcess(Index++,1);
  148.  
  149.                         WaitTime(0,CHAR_DELAY);
  150.                     }
  151.                     while(--Len);
  152.                 }
  153.                 else
  154.                     ConProcess(LocalBuffer,Len);
  155.             }
  156.             else
  157.             {
  158.                 Close(DataFeed);
  159.  
  160.                 DataFeed = NULL;
  161.             }
  162.         }
  163. #endif    /* DATAFEED */
  164.  
  165.         for(i = 0, Running = FALSE ; !MainTerminated && i < ResponseCount ; i++)
  166.         {
  167.             if(SignalSet & ResponseTable[i] . Mask)
  168.             {
  169.                 if((*ResponseTable[i] . Routine)())
  170.                     Running = TRUE;
  171.                 else
  172.                     SignalSet &= ~ResponseTable[i] . Mask;
  173.             }
  174.         }
  175.  
  176.         if(!SerialRunning && ReadPort && Status != STATUS_HOLDING && ProcessIO)
  177.         {
  178.             AddResponse(HandleSerial,SIG_SERIAL);
  179.  
  180.             SerialRunning = TRUE;
  181.  
  182.             SignalSet |= SIG_SERIAL;
  183.         }
  184.  
  185.         if(Running && !MainTerminated)
  186.             SignalSet |= SetSignal(0,ResponseMask) & ResponseMask;
  187.         else
  188.             break;
  189.     }
  190. }
  191.